﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace BigVisibleCountdown
{

    public partial class BigVisibleCountdownWindow : Window
    {

        //HACK: Yes, Yes, I know. This thing is a total kludge.
        //      I promise I will clean it up sometime and put
        //      the proper patterns in place.

        private DispatcherTimer _timer = new DispatcherTimer();
        CountdownTimer _countdownTimer = new CountdownTimer(0, 5, 0);

        public BigVisibleCountdownWindow()
        {
            InitializeComponent();
            _timer.Interval = new TimeSpan(0, 0, 1);
            _timer.Tick += Timer_Tick;

            SetCountdownContext();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            _countdownTimer.DecrementBy(_timer.Interval);
        }

        private void Start_Click(object sender, RoutedEventArgs e)
        {
            
            try
            {
                _countdownTimer =
                    new CountdownTimer(
                        int.Parse(txtHours.Text),
                        int.Parse(txtMinutes.Text),
                        int.Parse(txtSeconds.Text));
            }
            catch
            {
                MessageBox.Show(
                    "Well. I'm not sure what you're trying to countdown.\n \nHow about typing in some simple numbers for me and try again.",
                    "Hold up", MessageBoxButton.OK);
                
                return;
            }

            _timer.Start();
            DataContext = _countdownTimer;
            SetStartedView();
        }

        private void Stop_Click(object sender, RoutedEventArgs e)
        {
            _timer.Stop();
            SetStoppedView();
        }

        private void SetCountdownContext()
        {
            _timer.Stop();
            DataContext = _countdownTimer;
        }

        private void btnHoursUp_Click(object sender, RoutedEventArgs e)
        {
            _countdownTimer.IncrementBy(TimeSpan.FromHours(1));
        }

        private void btnHoursDown_Click(object sender, RoutedEventArgs e)
        {
            _countdownTimer.DecrementBy(TimeSpan.FromHours(1));
        }

        private void btnMinutesUp_Click(object sender, RoutedEventArgs e)
        {
            _countdownTimer.IncrementBy(TimeSpan.FromMinutes(1));
        }

        private void btnMinutesDown_Click(object sender, RoutedEventArgs e)
        {
            _countdownTimer.DecrementBy(TimeSpan.FromMinutes(1));
        }

        private void btnSecondsUp_Click(object sender, RoutedEventArgs e)
        {
            _countdownTimer.IncrementBy(TimeSpan.FromSeconds(1));
        }

        private void btnSecondsDown_Click(object sender, RoutedEventArgs e)
        {
            _countdownTimer.DecrementBy(TimeSpan.FromSeconds(1));
        }

        private void SetStartedView()
        {
            btnStart.IsEnabled = false;
            btnStop.IsEnabled = true;

            txtHours.Visibility = Visibility.Hidden;
            btnHoursUp.Visibility = Visibility.Hidden;
            btnHoursDown.Visibility = Visibility.Hidden;

            txtMinutes.Visibility = Visibility.Hidden;
            btnMinutesUp.Visibility = Visibility.Hidden;
            btnMinutesDown.Visibility = Visibility.Hidden;

            txtSeconds.Visibility = Visibility.Hidden;
            btnSecondsUp.Visibility = Visibility.Hidden;
            btnSecondsDown.Visibility = Visibility.Hidden;
        }

        private void SetStoppedView()
        {
            btnStop.IsEnabled = false;
            btnStart.IsEnabled = true;

            txtHours.Visibility = Visibility.Visible;
            btnHoursUp.Visibility = Visibility.Visible;
            btnHoursDown.Visibility = Visibility.Visible;

            txtMinutes.Visibility = Visibility.Visible;
            btnMinutesUp.Visibility = Visibility.Visible;
            btnMinutesDown.Visibility = Visibility.Visible;

            txtSeconds.Visibility = Visibility.Visible;
            btnSecondsUp.Visibility = Visibility.Visible;
            btnSecondsDown.Visibility = Visibility.Visible;
        }
    }
}
